package org.ithang.tools.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.ithang.tools.lang.JsonUtils;

import com.alibaba.fastjson.JSON;

public class HttpTools {

	private static CloseableHttpClient httpClient = HttpClientBuilder.create().build();
	
	public static String doGet(String url){
		CloseableHttpResponse response = null;
		HttpGet httpGet = new HttpGet(url);
		
		try{
			
			// 配置信息
			RequestConfig requestConfig = RequestConfig.custom()
								// 设置连接超时时间(单位毫秒)
								.setConnectTimeout(25000)
								// 设置请求超时时间(单位毫秒)
								.setConnectionRequestTimeout(25000)
								// socket读写超时时间(单位毫秒)
								.setSocketTimeout(25000)
								// 设置是否允许重定向(默认为true)
								.setRedirectsEnabled(true).build();
			 
			// 将上面的配置信息 运用到这个Get请求里
			httpGet.setConfig(requestConfig);
			
			// 由客户端执行(发送)Get请求
			response = httpClient.execute(httpGet);
			// 从响应模型中获取响应实体
			HttpEntity responseEntity = response.getEntity();
			if(200==response.getStatusLine().getStatusCode()){
				return EntityUtils.toString(responseEntity);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally {
			try {
				if (response != null) {
					response.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		return null;
	}
	
	public static String doPost(String url,Map<String,Object> params){
		// 创建Post请求
		HttpPost httpPost = new HttpPost(url);
		// 响应模型
		CloseableHttpResponse response = null;
		try {
				if(null!=params&&!params.isEmpty()){
					
					
					List<NameValuePair> pms = new ArrayList<NameValuePair>();
					 
		            // 遍历map，设置参数到list中
		            for (Map.Entry<String, Object> entry : params.entrySet()) {
		            	pms.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
		            }
		            
		            UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(pms, "utf-8");
		            //formEntity.setContentType("Content-Type:application/json");		
					// post请求是将参数放在请求体里面传过去的;这里将entity放入post请求体中
					httpPost.setEntity(formEntity);
			    }
			
				// 由客户端执行(发送)Post请求
				response = httpClient.execute(httpPost);
				// 从响应模型中获取响应实体
				HttpEntity responseEntity = response.getEntity();
		 
				if(200==response.getStatusLine().getStatusCode()){
					return EntityUtils.toString(responseEntity);
				}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
					try {
						if (response != null) {
							response.close();
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
		return null;
	}
	
	public static String doJSONPost(String url,Map<String,Object> params){
		// 创建Post请求
		HttpPost httpPost = new HttpPost(url);
		// 响应模型
		CloseableHttpResponse response = null;
		try {
			    if(null!=params&&!params.isEmpty()){
			    	String jsonString = JSON.toJSONString(params);
					StringEntity entity = new StringEntity(jsonString, "UTF-8");
					// post请求是将参数放在请求体里面传过去的;这里将entity放入post请求体中
					httpPost.setEntity(entity);
			    }
			    httpPost.setHeader("Content-Type", "application/json;charset=utf8");
				// 由客户端执行(发送)Post请求
				response = httpClient.execute(httpPost);
				// 从响应模型中获取响应实体
				HttpEntity responseEntity = response.getEntity();
		 
				if(200==response.getStatusLine().getStatusCode()){
					return EntityUtils.toString(responseEntity);
				}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
					try {
						if (response != null) {
							response.close();
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
		return null;
	}
}
